package cn.cnsuh.service.impl;


import cn.cnsuh.dao.GameDao;
import cn.cnsuh.dao.impl.GameDaoImpl;
import cn.cnsuh.domain.Game;
import cn.cnsuh.domain.PageBean;
import cn.cnsuh.service.GameService;

import java.util.List;

/**
 * @author Magic
 * @create 2020-05-19 0:12
 */
public class GameServiceImpl implements GameService {

    private GameDao gameDao = new GameDaoImpl();

    @Override
    public PageBean<Game> pageQuery(int currentPage, int pageSize, String gname) {
        //封装PageBean
        PageBean<Game> pb = new PageBean<Game>();
        //设置当前页码
        pb.setCurrentPage(currentPage);
        //设置每页显示的条数
        pb.setPageSize(pageSize);
        //设置总记录数
        int totalCount = gameDao.findTotalCount(gname);
        pb.setTotalCount(totalCount);

        //设置当前显示的数据的集合
        int start = (currentPage - 1) * pageSize;//开始的记录数
        List<Game> list = gameDao.findByPage(start, pageSize, gname);
        pb.setList(list);

        //设置总页数 = 总记录数/每页显示条数
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }
}
